-- personalVehicleMod by Aaronlelit
--------------------------------------------------
--
-- Installation:
--
--  Simply place vehicleRepairStation.lua file inside the
--  \scripts\addins\ directory in your base GTAV
--  install location.
--
--------------------------------------------------
--    
-- Requirements:
--
--Script Hook V :
--https://www.gta5-mods.com/tools/script-hook-v
--LUA Plugin For Script Hook V :
--https://www.gta5-mods.com/tools/lua-plugin-for-script-hook-v
--
--------------------------------------------------
--Information :
--
--This mod let you choice your main vehicle!
--Press CTRL + U for set your vehicle !
--Press CTRL + E for spawn your vehicle !
--
--Please if you re-upload my mod on other website credit me !
--------------------------------------------------

local personalVehicleMod = {}

-- Setting

personalVehicleMod.version = "0.3"
personalVehicleMod.canDeleteVehicle = false
local personalPlayerVehicle = nil

-- Keyboard

personalVehicleMod.key = {}
personalVehicleMod.key.get = 69 -- "E Key"
personalVehicleMod.key.set = 85 -- "U Key"

-- [CODE] --

-- Basic Functions

function personalVehicleMod.tick()

	-- GET VEHICLE
	
	if ( get_key_pressed(personalVehicleMod.key.get) and get_key_pressed(17) ) then
		local configFile = io.open("personalVehicleMod.ini", "r")
		if configFile then
			personalVehicleMod.getVehicle()
		else
			personnalVehicleMod.notify("You haven't a saved car!")
		end
		configFile:close()
	end
	
	-- SET VEHICLE
	
	if ( get_key_pressed(personalVehicleMod.key.set) and get_key_pressed(17) ) then
		personalVehicleMod.setVehicle()
	end
	
end

-- Functions

function personalVehicleMod.getVehicle()
	
	if (personalVehicleMod.canDeleteVehicle) then VEHICLE.DELETE_VEHICLE(personalPlayerVehicle) end
	
	personalVehicleMod.notify("Your vehicle has just been delivered to you!")
	personalVehicleMod.pay(1000)
	
	-- Get Vehicle Information
	
	file = io.open("personalVehicleMod.ini", "r")
	
	local vals = {}
	for line in file:lines() do
		table.insert(vals, line);
	end
	
	file:close()

	local personalPlayerVehicleHash = vals[1]
	local colr = vals[2]
	local colg = vals[3]
	local colb = vals[4]
	local col2r = vals[5]
	local col2g = vals[6]
	local col2b = vals[7]
	
	-- Apply Vehicle Information on vehicle

	STREAMING.REQUEST_MODEL(personalPlayerVehicleHash)
	while(not STREAMING.HAS_MODEL_LOADED(personalPlayerVehicleHash)) do
		wait(50)
	end
	
	local location = ENTITY.GET_ENTITY_COORDS(PLAYER.PLAYER_PED_ID(), nil)
	
	personalPlayerVehicle = VEHICLE.CREATE_VEHICLE(personalPlayerVehicleHash, location.x + 3, location.y + 1, location.z, 90, false, true)
	VEHICLE.SET_VEHICLE_MOD_KIT(personalPlayerVehicle, 0)
	VEHICLE.SET_VEHICLE_ON_GROUND_PROPERLY(personalPlayerVehicle)
	VEHICLE.SET_VEHICLE_CUSTOM_PRIMARY_COLOUR(personalPlayerVehicle, colr, colg, colb)
	VEHICLE.SET_VEHICLE_CUSTOM_SECONDARY_COLOUR(personalPlayerVehicle, col2r, col2g, col2b)
	
	
	VEHICLE.SET_VEHICLE_WHEEL_TYPE(personalPlayerVehicle, 	vals[8]);
	VEHICLE.SET_VEHICLE_WINDOW_TINT(personalPlayerVehicle, 	vals[9]);
	
	
	VEHICLE.SET_VEHICLE_MOD(personalPlayerVehicle, 0, tonumber(vals[10]), false)
	VEHICLE.SET_VEHICLE_MOD(personalPlayerVehicle, 1, tonumber(vals[11]), false)
	VEHICLE.SET_VEHICLE_MOD(personalPlayerVehicle, 2, tonumber(vals[12]), false)
	VEHICLE.SET_VEHICLE_MOD(personalPlayerVehicle, 3, tonumber(vals[13]), false)
	VEHICLE.SET_VEHICLE_MOD(personalPlayerVehicle, 4, tonumber(vals[14]), false)
	VEHICLE.SET_VEHICLE_MOD(personalPlayerVehicle, 5, tonumber(vals[15]), false)
	VEHICLE.SET_VEHICLE_MOD(personalPlayerVehicle, 6, tonumber(vals[16]), false)
	VEHICLE.SET_VEHICLE_MOD(personalPlayerVehicle, 7, tonumber(vals[17]), false)
	VEHICLE.SET_VEHICLE_MOD(personalPlayerVehicle, 8, tonumber(vals[18]), false)
	VEHICLE.SET_VEHICLE_MOD(personalPlayerVehicle, 9, tonumber(vals[19]), false)
	VEHICLE.SET_VEHICLE_MOD(personalPlayerVehicle, 10, tonumber(vals[20]), false)
	VEHICLE.SET_VEHICLE_MOD(personalPlayerVehicle, 11, tonumber(vals[21]), false)
	VEHICLE.SET_VEHICLE_MOD(personalPlayerVehicle, 12, tonumber(vals[22]), false)
	VEHICLE.SET_VEHICLE_MOD(personalPlayerVehicle, 13, tonumber(vals[23]), false)
	VEHICLE.SET_VEHICLE_MOD(personalPlayerVehicle, 14, tonumber(vals[24]), false)
	VEHICLE.SET_VEHICLE_MOD(personalPlayerVehicle, 15, tonumber(vals[25]), false)
	VEHICLE.SET_VEHICLE_MOD(personalPlayerVehicle, 16, tonumber(vals[26]), false)
	VEHICLE.SET_VEHICLE_MOD(personalPlayerVehicle, 18, stringtobool(vals[27]), false)
	VEHICLE.SET_VEHICLE_MOD(personalPlayerVehicle, 22, stringtobool(vals[28]), false)
	VEHICLE.SET_VEHICLE_MOD(personalPlayerVehicle, 23, tonumber(vals[29]), false)
	VEHICLE.SET_VEHICLE_MOD(personalPlayerVehicle, 24, tonumber(vals[30]), false)
	
	
	--VEHICLE.SET_VEHICLE_MOD_COLOR_1(personalPlayerVehicle, vals[31], 0, 0)
	--VEHICLE.SET_VEHICLE_MOD_COLOR_2(personalPlayerVehicle, vals[32], 0)
	
	personalVehicleMod.canDeleteVehicle = true
	
	wait(1000)

end

function personalVehicleMod.setVehicle()
	
	personalVehicleMod.notify("You just select your vehicle !")
	local playerVehicle = PED.GET_VEHICLE_PED_IS_IN(PLAYER.PLAYER_PED_ID(), false)
	
	local vals = {}
	
	table.insert(	vals,	VEHICLE.GET_VEHICLE_WHEEL_TYPE(playerVehicle)  	 	)	--Wheels
	table.insert(	vals,	VEHICLE.GET_VEHICLE_WINDOW_TINT(playerVehicle) 	 	)	--Film
	table.insert(	vals,	VEHICLE.GET_VEHICLE_MOD(playerVehicle, 0)   	 	)	--Spoilers
	table.insert(	vals,	VEHICLE.GET_VEHICLE_MOD(playerVehicle, 1)   	 	)	--Front Bumper
	table.insert(	vals,	VEHICLE.GET_VEHICLE_MOD(playerVehicle, 2)   	 	)	--Rear Bumper
	table.insert(	vals,	VEHICLE.GET_VEHICLE_MOD(playerVehicle, 3)   	 	)	--Side Skirt
	table.insert(	vals,	VEHICLE.GET_VEHICLE_MOD(playerVehicle, 4)   	 	)	--Exhaust
	table.insert(	vals,	VEHICLE.GET_VEHICLE_MOD(playerVehicle, 5)   	 	)	--Frame
	table.insert(	vals,	VEHICLE.GET_VEHICLE_MOD(playerVehicle, 6)   	 	)	--Grille
	table.insert(	vals,	VEHICLE.GET_VEHICLE_MOD(playerVehicle, 7)   	 	)	--Hood
	table.insert(	vals,	VEHICLE.GET_VEHICLE_MOD(playerVehicle, 8)   	 	)	--Fender
	table.insert(	vals,	VEHICLE.GET_VEHICLE_MOD(playerVehicle, 9)   	 	)	--Right Fender
	table.insert(	vals,	VEHICLE.GET_VEHICLE_MOD(playerVehicle, 10)  	 	)	--Roof
	
	table.insert(	vals,	VEHICLE.GET_VEHICLE_MOD(playerVehicle, 11)  	 	)	--Engine
	table.insert(	vals,	VEHICLE.GET_VEHICLE_MOD(playerVehicle, 12)  	 	)	--Brakes
	table.insert(	vals,	VEHICLE.GET_VEHICLE_MOD(playerVehicle, 13)  	 	)	--Transmission
	table.insert(	vals,	VEHICLE.GET_VEHICLE_MOD(playerVehicle, 14) 	 		)	--Horns
	table.insert(	vals,	VEHICLE.GET_VEHICLE_MOD(playerVehicle, 15)  	 	)	--Suspension
	table.insert(	vals,	VEHICLE.GET_VEHICLE_MOD(playerVehicle, 16)  	 	)	--Armor
				 
	table.insert(	vals,	VEHICLE.IS_TOGGLE_MOD_ON(playerVehicle, 18)			)	--Turbo
	table.insert(	vals,	VEHICLE.IS_TOGGLE_MOD_ON(playerVehicle, 22)			)	--Xenon
				 
	table.insert(	vals,	VEHICLE.GET_VEHICLE_MOD(playerVehicle, 23)	  		)	--Front Wheels
	table.insert(	vals,	VEHICLE.GET_VEHICLE_MOD(playerVehicle, 24)	  		)	--Back Wheels (BIKES ONLY)
	
	local paintshaderid = VEHICLE.GET_VEHICLE_MOD_COLOR_1(playerVehicle, 0, 0, 0)
	table.insert( vals, paintshaderid)
	local paintshaderid2 = VEHICLE.GET_VEHICLE_MOD_COLOR_2(playerVehicle, 0, 0)
	table.insert( vals, paintshaderid2)

	local colr, colg, colb = VEHICLE.GET_VEHICLE_CUSTOM_PRIMARY_COLOUR(playerVehicle,0,0,0)
	local col2r, col2g, col2b = VEHICLE.GET_VEHICLE_CUSTOM_SECONDARY_COLOUR(playerVehicle,0,0,0)
	personalVehicleName = ENTITY.GET_ENTITY_MODEL(playerVehicle)
	
	file = io.open("personalVehicleMod.ini", "w")

	file:write(personalVehicleName, "\n")
	file:write(colr, "\n")
	file:write(colg, "\n")
	file:write(colb, "\n")
	file:write(col2r, "\n")
	file:write(col2g, "\n")
	file:write(col2b, "\n")
	
	local i = 1
	while i < 25 do
		file:write(tostring(vals[i]), "\n")
		i = i + 1
	end
	
	file:close()
	
	wait(1000)
	
end

function personalVehicleMod.pay(money)
	if (PED.IS_PED_MODEL(PLAYER.PLAYER_PED_ID(), GAMEPLAY.GET_HASH_KEY("player_zero"))) then model = 0
	elseif (PED.IS_PED_MODEL(PLAYER.PLAYER_PED_ID(), GAMEPLAY.GET_HASH_KEY("player_one"))) then model = 1                                         
	elseif (PED.IS_PED_MODEL(PLAYER.PLAYER_PED_ID().GET_HASH_KEY("player_two"))) then model = 2 end
	local statname = "SP"..model.."_TOTAL_CASH"
	local hash = GAMEPLAY.GET_HASH_KEY(statname)
	local bool, val = STATS.STAT_GET_INT(hash, 0, -1)
	STATS.STAT_SET_INT(hash, val - money, true)
end

function personalVehicleMod.notify(msg)
	UI._SET_NOTIFICATION_TEXT_ENTRY("STRING")
	UI._ADD_TEXT_COMPONENT_STRING(msg)
	UI._DRAW_NOTIFICATION(false, false)
end

local function stringtobool(stringVar)
	if (stringVar == "true") then
		return true
	else
		return false
	end
end

return personalVehicleMod